<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>护理记录管理</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/common.js"></script>
    <script src="js/jsrender.js"></script>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        var pageNum = 1		// 记录当前页面
        var method = "showAll"	// 记录当前方法
        var pages	// 记录页面总数
        const pageSize = 5 //页面行数
        $(function () {
            var today = new Date()
            var nowDate = today.getFullYear() +
                "-" + (today.getMonth() + 1 < 10 ? "0" + (today.getMonth() + 1) : today.getMonth() + 1) +
                "-" + (today.getDate() < 10 ? "0" + today.getDate() : today.getDate())+
                "T" + (today.getHours() < 10 ? "0" + today.getHours() : today.getHours())+
                ":" + (today.getMinutes() < 10 ? "0" + today.getMinutes() : today.getMinutes())
            $("#nursingrecordtime_insert").val(nowDate)

            function timeReset(){
                $("#nextTime_find").val((nowDate.split("T")[0]))
                $("#preTime_find").val("1970-01-01")
            }

            timeReset()

            $.views.converters("dateformat",function(val){
                var mydate = new Date(val)
                var y = mydate.getFullYear()
                var M = mydate.getMonth() + 1
                var d = mydate.getDate()
                var H = mydate.getHours()
                var m = mydate.getMinutes()
                return y + "-" + (M < 10 ? "0" + M : M) + "-" + (d < 10 ? "0" + d : d) +" "+H+":"+m
            })

            function showAll() {
                //请求所有护理记录信息
                $.ajax({
                    url: GC.bctx + "nursingRecord/list",
                    type: "GET",
                    data: {pageNum:pageNum,pageSize:pageSize},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success: function (r) {
                        if (r.successed) {
                            $("#nursingRecords").html($("#nursingRecordsTemplate").render(r.data.list))
                            $("#pagination").html($("#paginationTemplate").render(r.data))
                            pages = r.data.pages
                            method = "showAll"
                        } else {
                            window.location.href = "login.html"
                        }
                    }
                })
            }


            function find() {
                var preTime_temp = (new Date($("#preTime_find").val())).getTime()
                var nextTime_temp = (new Date($("#nextTime_find").val())).getTime()
                $.ajax({
                    url: GC.bctx + "nursingRecord/find",
                    type: "GET",
                    data: {preTime:preTime_temp,nextTime:nextTime_temp,pageNum:pageNum,pageSize:pageSize},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success: function (r){
                        if(r.successed){
                            $("#nursingRecords").html($("#nursingRecordsTemplate").render(r.data.list))
                            $("#pagination").html($("#paginationTemplate").render(r.data))
                            pages = r.data.pages
                            method = "find"
                        }else{
                            window.location.href="login.html"
                        }
                    }
                })
            }


            //显示所有护理记录级别信息
            showAll()

            //查询
            $("form").submit(function () {
                pageNum = 1
                find()
                return false
            })


            $("#findAllNursingRecord").click(function(){
                pageNum=1
                showAll()
            })



            //显示详细信息
            $(document).on("click", "[name=updateNursingRecord]", function () {
                var id = $(this).attr("data-id")
                $.ajax({
                    url: GC.bctx + "nursingRecord/" + id,
                    type: "GET",
                    headers: {"Authorization": localStorage.getItem("token")},
                    success: function (r) {
                        if (r.successed) {
                            $("#nursingRecordid_update").val(r.data.nursingrecordid)
                            $("#nursingcontentid_update").val(r.data.nursingcontentid)
                            $("#customerid_update").val(r.data.customerid)
                            $("#nurseid_update").val(r.data.nurseid)
                            var date = new Date(r.data.nursingrecordtime)
                            var recordDate = date.getFullYear() +
                                "-" + (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) +
                                "-" + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate())+
                                "T" + (date.getHours() < 10 ? "0" + date.getHours() : date.getHours())+
                                ":" + (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes())
                            $("#nursingrecordtime_update").val(recordDate)
                            $("#updateModal").modal("show")
                        } else {
                            window.location.href = "login.html"
                        }
                    }
                })

            })

            // 分页组件点击跳转，两种情况
            $(document).on("click", "[name=jump]", function() {
                pageNum = this.innerHTML	// 获取<a>的内容
                method == "showAll" ? showAll() : find()
            })

            // 点击上一页按钮
            $(document).on("click", "[name=previous]", function() {
                if(pageNum-1 > 0) {
                    pageNum = pageNum-1
                    method == "showAll" ? showAll() : find()
                } else {
                    alert("当前页为第1页")
                }
            })

            // 点击下一页按钮
            $(document).on("click", "[name=next]", function() {
                if(pageNum+1 <= pages) {
                    pageNum = pageNum+1
                    method == "showAll" ? showAll() : find()
                } else {
                    alert("当前页为最后1页")
                }
            })
        })


        //插入
        function insert_form() {
            var temp =$("#nursingrecordtime_insert").val().split("T")
            var timeToInsert=(new Date(temp[0]+" "+temp[1])).getTime()
            var nursingcontentid_temp = $('#nursingcontentid_insert').val()
            var customerid_temp = $('#customerid_insert').val()
            var nurseid_temp = $('#nurseid_insert').val()

            if (customerid_temp==null||customerid_temp<1||nursingcontentid_temp==null||nursingcontentid_temp<1||nurseid_temp==null||nurseid_temp<1) {
                alert('三个编号任一个必须不为空且大于零！');
                return false;
            }

            // 异步提交数据
            $.ajax(
                {
                    url: GC.bctx + "nursingRecord/insert",
                    type: "POST",
                    data: {nursingcontentid:nursingcontentid_temp,customerid:customerid_temp,nurseid:nurseid_temp,nursingrecordtime:timeToInsert},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success: function (r) {
                        if (r.successed) {
                            //新增成功，转向显示所有部门页面
                            alert(r.message)
                            window.location.href = "nursingRecord.html"
                        } else {
                            //新增失败，显示失败信息
                            alert(r.message)
                        }
                    }
                });

            return false;
        }

        //修改
        function update_form() {
            var temp =$("#nursingrecordtime_update").val().split("T")
            var timeToInsert=(new Date(temp[0]+" "+temp[1])).getTime()

            var nursingcontentid_temp = $('#nursingcontentid_update').val()
            var customerid_temp = $('#customerid_update').val()
            var nurseid_temp = $('#nurseid_update').val()
            var nursingRecordid_temp =$("#nursingRecordid_update").val()
            alert(nursingRecordid_temp)

            if (customerid_temp==null||customerid_temp<1||nursingcontentid_temp==null||nursingcontentid_temp<1||nurseid_temp==null||nurseid_temp<1) {
                alert('三个编号任一个必须不为空且大于零！');
                return false;
            }

            // 异步提交数据
            $.ajax(
                {
                    url: GC.bctx + "nursingRecord/update/"+nursingRecordid_temp,
                    type: "PUT",
                    data: {nursingrecordID:nursingRecordid_temp,nursingcontentid:nursingcontentid_temp,customerid:customerid_temp,nurseid:nurseid_temp,nursingrecordtime:timeToInsert},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success: function (r) {
                        if (r.successed) {
                            //新增成功，转向显示所有部门页面
                            alert(r.message)
                            window.location.href = "nursingRecord.html"
                        } else {
                            //新增失败，显示失败信息
                            alert(r.message)
                        }
                    }
                });

            return false;
        }
    </script>
</head>



<body>

<div class="float-left">
    <form action="find" method="get" class="container-fluid text-center">
        <label>起始时间</label>
        <input name="preTime" id="preTime_find" type="date">
        <label>终止时间</label>
        <input name="nextTime" id="nextTime_find" type="date">
        <input class="btn btn-info" type="submit" value="查 询"/>
        <a id="findAllNursingRecord" class="btn btn-info" >查询所有</a>
    </form>
</div>
<!-- 按钮触发模态框 -->
<button class="btn btn-primary " data-toggle="modal" data-target="#insertModal">✚ 添加</button>
<!-- 模态框（Modal） -->
<form method="post" action="" class="form-horizontal" role="form" id="form_insert" onsubmit="return insert_form()"
      style="margin: 20px;">
    <div class="modal fade" id="insertModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true" data-backdrop="static" data-keyboard="false">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myModalLabel">新增护理记录信息</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="nursingcontentid_insert" class="col-sm-3 control-label">护理项目编号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="nursingcontentid_insert" name="nursingcontentid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="customerid_insert" class="col-sm-3 control-label">顾客编号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="customerid_insert" name="customerid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="nurseid_insert" class="col-sm-3 control-label">护士编号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="nurseid_insert" name="nurseid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="nursingrecordtime_insert" class="col-sm-3 control-label">护理时间</label>
                            <div class="col-sm-9">
                                <input type="datetime-local" class="form-control" id="nursingrecordtime_insert" name="nursingrecordtime">
                            </div>
                        </div>



                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="submit" class="btn btn-primary">
                        提交
                    </button>
                    <span id="tip"> </span>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</form>

<!-- 修改信息模态框（Modal） -->
<form method="post" action="" class="form-horizontal" role="form" id="form_update" onsubmit="return update_form()"
      style="margin: 20px;">
    <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
         aria-hidden="true" data-backdrop="static" data-keyboard="false">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="myModalLabel_upate">修改护理等级信息</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <div class="col-sm-9">
                                <input type="hidden" class="form-control" id="nursingRecordid_update" name="nursingrecordid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="nursingcontentid_update" class="col-sm-3 control-label">护理项目编号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="nursingcontentid_update" name="nursingcontentid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="customerid_update" class="col-sm-3 control-label">顾客编号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="customerid_update" name="customerid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="nurseid_update" class="col-sm-3 control-label">护士编号</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="nurseid_update" name="nurseid">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="nursingrecordtime_update" class="col-sm-3 control-label">护理时间</label>
                            <div class="col-sm-9">
                                <input type="datetime-local" class="form-control" id="nursingrecordtime_update" name="nursingrecordtime">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="submit" class="btn btn-primary">
                        提交
                    </button>
                    <span id="tip_update"> </span>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
</form>


<script id="nursingRecordsTemplate" type="text/jx-jsrender">
		<tr align="center">
			<td>{{:nursingrecordid}}</td>
			<td>{{:nursingcontentprojectname}}</td>
			<td>{{:customername}}</td>
			<td>{{:nursename}}</td>
			<td>{{dateformat:nursingrecordtime}}</td>
			<td align="center">
			    <button name="updateNursingRecord" class="btn btn-info" data-id="{{:nursingrecordid}}">修改</button>
<!--				<button name="deleteNursingRecord" class="btn btn-danger" data-id="{{:nursingrecordid}}">删除</button>-->
			</td>
		</tr>


</script>

<table class="table table-bordered">
    <tr>
        <th class="text-center" width="15%">序号</th>
        <th class="text-center" width="20%">护理记录项目名称</th>
        <th class="text-center" width="15%">顾客姓名</th>
        <th class="text-center" width="15%">护士姓名</th>
        <th class="text-center" width="20%">护理时间</th>
        <th class="text-center" width="15%">操作</th>
    </tr>
    <tbody id="nursingRecords"></tbody>
</table>

<!--分页-->
<div align="center">
    <ul class="pagination" id="pagination"></ul>
    <script id="paginationTemplate" type="text/x-jsrender">
		<li class="page-item"><a class="page-link" href="javascript:void(0);" name="previous">&laquo;</a></li>
		{{for navigatepageNums}}
		<li class="page-item"><a class="page-link" href="javascript:void(0);" name="jump">{{:#data}}</a></li>
		{{/for}}
		<li class="page-item"><a class="page-link" href="javascript:void(0);" name="next">&raquo;</a></li>
	</script>
</div>

</body>
</html>